<?php

namespace app\admin\model\zhongtai;

use think\Model;

class ShoproOrderItem extends Model
{
// 表名
    protected $table = 'shopro_order_item';
    // 设置当前模型的数据库连接
    protected $connection = 'db_zhongtai';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    /**
     * 获取最新的更新时间
     */
    public function getUpTime($manage_id)
    {
        $updatetime = $this->where('manage_id', $manage_id)->order("zt_time desc")->value("zt_time");
        return $updatetime ?? 0;
    }

    public function upData($data, $manage_id)
    {
        if ($data) {
            $con = count($data);
            foreach ($data as $k => $v) {
                echo $k . PHP_EOL;
                if (($con - 1) == $k) {
                    $zttime = date('Y-m-d H:i:s',strtotime($v['zt_time'])+1);
                }else{
                    $zttime = $v['zt_time'];
                }
                $user_data = (new User())->getUserStatus($manage_id,$v['user_id']);
                if(!$user_data) continue;
                $in_data = [
                    "manage_id" => $manage_id,
                    "order_item_id" => $v['id'],
                    "user_id" => $v['user_id'],
                    "order_id" => $v['order_id'],
                    "goods_id" => $v['goods_id'],
                    "goods_type" => $v['goods_type'],
                    "goods_sku_price_id" => $v['goods_sku_price_id'],
                    "activity_id" => $v['activity_id'],
                    "activity_type" => $v['activity_type'],
                    "item_goods_sku_price_id" => $v['item_goods_sku_price_id'],
                    "goods_sku_text" => $v['goods_sku_text'],
                    "goods_title" => $v['goods_title'],
                    "goods_image" => $v['goods_image'],
                    "goods_original_price" => $v['goods_original_price'],
                    "discount_fee" => $v['discount_fee'],
                    "goods_price" => $v['goods_price'],
                    "goods_num" => $v['goods_num'],
                    "goods_weight" => $v['goods_weight'],
                    "pay_price" => $v['pay_price'],
                    "dispatch_status" => $v['dispatch_status'],
                    "dispatch_fee" => $v['dispatch_fee'],
                    "dispatch_type" => $v['dispatch_type'],
                    "dispatch_id" => $v['dispatch_id'],
                    "store_id" => $v['store_id'],
                    "aftersale_status" => $v['aftersale_status'],
                    "comment_status" => $v['comment_status'],
                    "refund_status" => $v['refund_status'],
                    "refund_fee" => $v['refund_fee'],
                    "refund_msg" => $v['refund_msg'],
                    "express_name" => $v['express_name'],
                    "express_code" => $v['express_code'],
                    "express_no" => $v['express_no'],
                    "ext" => $v['ext'],
                    "createtime" => $v['createtime'],
                    "updatetime" => $v['updatetime'],
                    "deletetime" => $v['deletetime'],
                    "is_share" => $v['is_share'],
                    "zt_time" => $zttime,
                ];
                $user_data = $this->where([
                    'order_item_id' =>  $v['id'],
                    'manage_id' => $manage_id,
                ])->find();
                if ($user_data) {
                    $this->where([
                        'order_item_id' =>  $v['id'],
                        'manage_id' => $manage_id,
                    ])->update($in_data);
                } else {
                    $this->insert($in_data);
                }
            }
        }
        echo "shopro_order_item表更新完成";
    }
}
